package com.zhlb.zfba.modules.per.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zhlb.zfba.common.utils.DateUtil;
import com.zhlb.zfba.common.utils.IdGen;
import com.zhlb.zfba.modules.card.bean.CardRegInfo;
import com.zhlb.zfba.modules.card.service.CardRegInfoService;
import com.zhlb.zfba.modules.per.bean.PoliceInfo;
import com.zhlb.zfba.modules.per.bean.RoomInfo;
import com.zhlb.zfba.modules.per.bean.SuspectInfo;
import com.zhlb.zfba.modules.per.dao.PoliceInfoDao;

@Service
@Transactional(readOnly = true)
public class PoliceInfoService {
	@Autowired 
	PoliceInfoDao policeInfoDao;
	@Autowired
	CardRegInfoService cardRegInfoService;
	public PoliceInfo get(String id) {
		PoliceInfo policeInfo=policeInfoDao.get(id);
		if(policeInfo==null){
			policeInfo= new  PoliceInfo();
		}
		if(policeInfo.getRoomInfo()==null){
			policeInfo.setRoomInfo(new RoomInfo());
		}
		return policeInfo;
	}
	
	public List<PoliceInfo> findList(PoliceInfo policeInfo) {
		return policeInfoDao.findList(policeInfo);
	}
	
//	public Page<PoliceInfo> findPage( PoliceInfo taskJobConf) {
//		return super.findPage(page, taskJobConf);
//	}
	
	@Transactional(readOnly = false)
	public void save(PoliceInfo policeInfo) {
		CardRegInfo cardRegInfo=null;
		if(StringUtils.isNoneBlank(policeInfo.getPoliceCardNo1())){
			CardRegInfo cardRegInfoQuery = new CardRegInfo();
			cardRegInfoQuery.setCardNo1(policeInfo.getPoliceCardNo1());
			 cardRegInfo=cardRegInfoService.getByCardNo(cardRegInfoQuery);
		}
		if(cardRegInfo!=null){
			policeInfo.setPoliceCardID(cardRegInfo.getCardID());
			policeInfo.setPoliceCardNo2(cardRegInfo.getCardNo2());
		}
		if(StringUtils.isBlank(policeInfo.getInfoGUID())){
			policeInfo.setInfoGUID(IdGen.uuid());
			policeInfo.setInTime(DateUtil.dateToStr(new Date(), "yyyy-MM-dd HH:mm:ss"));
			policeInfo.setIsLeave("0");
			policeInfoDao.insert(policeInfo);
			
		}else{
			PoliceInfo updateEntity=this.get(policeInfo.getInfoGUID());
			//updateEntity.setInfoGUID(policeInfo.getInfoGUID());
			updateEntity.setPoliceName(policeInfo.getPoliceName());
			updateEntity.setPoliceNo(policeInfo.getPoliceNo());
			updateEntity.setPoliceOrg(policeInfo.getPoliceOrg());
			updateEntity.setPoliceBringCount(policeInfo.getPoliceBringCount());
			updateEntity.setPoliceBringInfo(policeInfo.getPoliceBringInfo());
			updateEntity.setPoliceReason(policeInfo.getPoliceReason());
			updateEntity.setPoliceCardID(policeInfo.getPoliceCardID());
			updateEntity.setPoliceCardNo1(policeInfo.getPoliceCardNo1());
			updateEntity.setPoliceCardNo2(policeInfo.getPoliceCardNo2());
			updateEntity.setInquestRoomNo(policeInfo.getInquestRoomNo());
			//updateEntity.setInTime(policeInfo.getInTime());
			updateEntity.setFillInName(policeInfo.getFillInName());
			//updateEntity.setCurPostion(policeInfo.getCurPostion());
			//updateEntity.setCurStatus(policeInfo.getCurStatus());
			//updateEntity.setIsLeave(policeInfo.getIsLeave());
			//updateEntity.setLeaveTime(policeInfo.getLeaveTime());
			//updateEntity.setLeaveReason(policeInfo.getLeaveReason());
			
			policeInfoDao.update(updateEntity);
		 }
	}
	
	@Transactional(readOnly = false)
	public void delete(String ids) {
		String[] idArr=null;
		if(StringUtils.isNotBlank(ids)){
			idArr=ids.split(",");
			for(String id:idArr){
				policeInfoDao.delete(id);
			}
		}
		
	}
	@Transactional(readOnly = false)
	public void updateIsLeave(PoliceInfo policeInfo){
		policeInfo.setIsLeave("1");
		policeInfo.setLeaveTime(DateUtil.dateToStr(new Date(), "yyyy-MM-dd HH:mm:ss"));
		policeInfoDao.updateIsLeaveOfPoliceInfo(policeInfo);
	}
}
